package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.SoulTest.*;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.SoulTest.QuestionNaireService;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class QuestionNaireManager {
    @DubboReference
    private QuestionNaireService questionNaireService;

    public ResponseEntity findTestSoul() {
        System.out.println("findTestSoul");
        List<QuestionNaire> questionNaireList = questionNaireService.findQuestionNaireList();
//        System.out.println(questionNaireList);
        List<QuestionNaireVo> voList = new ArrayList<>();
        for (QuestionNaire questionNaire : questionNaireList) {
            QuestionNaireVo vo = new QuestionNaireVo();
            vo = vo.setVo(questionNaire);
//            vo.setQuestions(null);
            voList.add(vo);
        }
        //查询用户写了哪几张表 还要根据写了几张表返回相应的报告id
        Long userId = UserHolder.getUserId();
        //Integer soulState = questionNaireService.findUserSoulState(userId);
        UserSoulScore userSoulScore = questionNaireService.findUserSoulScore(userId);
        List<ReportState> reportStateList = questionNaireService.findReportStateByUserId(UserHolder.getUserId());
        System.out.println("reportStateList--"+reportStateList);
        if (userSoulScore.getFirstResuldId() == null) {
            voList.get(1).setIsLock(1);
            voList.get(2).setIsLock(1);
        } else if (userSoulScore.getThirdResuldId() == null && userSoulScore.getSecondResuldId() == null) {
            voList.get(2).setIsLock(1);

            voList.get(0).setReportId(reportStateList.get(0).getId().toHexString());
        } else if (userSoulScore.getThirdResuldId() == null) {
            voList.get(0).setReportId(reportStateList.get(0).getId().toHexString());
            voList.get(1).setReportId(reportStateList.get(1).getId().toHexString());
        } else {
            voList.get(0).setReportId(reportStateList.get(0).getId().toHexString());
            voList.get(1).setReportId(reportStateList.get(1).getId().toHexString());
            voList.get(2).setReportId(reportStateList.get(2).getId().toHexString());
        }

        return ResponseEntity.ok(voList);

    }

    public ResponseEntity submitQuestionnaire(Answers answers) {
        System.out.println(answers);
        List<Answer> answerList = answers.getAnswers();
        //先得到试卷分数
        Integer score = 0;
        for (Answer answer : answerList) {
            Integer integer = Integer.valueOf(answer.getOptionId());
            score += integer;
        }

        String questionId = answerList.get(0).getQuestionId();
        List<QuestionNaire> questionNaireList = questionNaireService.findQuestionNaireList();
        System.out.println(questionNaireList);
        //判断哪张试卷
        int questionNaireNumber = 0;
        for (int i = 0; i < questionNaireList.size(); i++) {
            if (questionNaireList.get(i).getQuestions().get(0).getId().toHexString().equals(questionId)) {
                questionNaireNumber = i + 1;
            }
        }

        System.out.println("questionNaireNumber------" + questionNaireNumber);

        String reportId = questionNaireService.setUserScore(questionNaireNumber, UserHolder.getUserId(), score);
//        System.out.println(reportId);
        //记录是哪张问卷和相应的性格id
        ReportState reportState = new ReportState();
        reportState.setReportNum(questionNaireNumber);
        reportState.setUserId(UserHolder.getUserId());
        reportState.setReportId(reportId);
        String reportNumId = questionNaireService.saveReportState(reportState);
        System.out.println("reportNumId" + reportNumId);
        return ResponseEntity.ok(reportNumId);
    }

    @DubboReference
    private UserInfoService userInfoService;

    public ResponseEntity soulReport(String id) {
        Long userId = UserHolder.getUserId();
        UserSoulScore userSoulScore = questionNaireService.findUserSoulScore(userId);
        System.out.println("11111userSoulScore--"+userSoulScore);
        ReportState reportState = questionNaireService.findReportState(id);
//提前建立对象防止以下if else报错
        List<SimilarOne> similarOnes = new ArrayList();
        SoulResultVo soulResultVo = new SoulResultVo();
        SoulResult soulResult;
        if (reportState == null) {
            soulResult = questionNaireService.findReport(id, userId);
        } else {
            System.out.println("进入不为空寻找相似用户");
            //不为空寻找相似用户
            String reportId = reportState.getReportId();
            soulResult = questionNaireService.findReport(reportId, userId);
            System.out.println("soulResult" + soulResult);
            //判断目前是哪张评价表
            Integer reportNum = reportState.getReportNum();

            List<Long> similarYou = new ArrayList<>();
            System.out.println("reportNum"+reportNum);

            switch (reportNum) {
                case 1: {
                    System.out.println("case1-userSoulScore"+userSoulScore);
                    System.out.println("userSoulScore.getFirstScore()"+userSoulScore.getFirstScore());
                    Integer firstScore = userSoulScore.getFirstScore();
                    System.out.println("firstScore--"+firstScore);
                    similarYou = questionNaireService.findSimilarUser(userId, firstScore, 1);
                    break;
                }
                case 2: {
                    similarYou = questionNaireService.findSimilarUser(userId, userSoulScore.getSecondScore(), 2);
                    break;
                }
                case 3: {
                    similarYou = questionNaireService.findSimilarUser(userId, userSoulScore.getThirdScore(), 3);
                    break;
                }
            }
            System.out.println("similarYou"+similarYou);
            //封装
            if (CollectionUtil.isNotEmpty(similarYou)) {
                for (Long findId : similarYou) {

                    UserInfo userInfo = userInfoService.findByUserId(findId);
                    if (userInfo.getId().equals(UserHolder.getUserId())) {
                        continue;
                    }
                    SimilarOne similarOne = new SimilarOne();
                    similarOne.setAvatar(userInfo.getAvatar());
                    similarOne.setId(userInfo.getId().intValue());
                    similarOnes.add(similarOne);

                }

            }
        }//    if else 完毕
        BeanUtil.copyProperties(soulResult, soulResultVo);

        //不够就随机假数据
        if (similarOnes.size() <= 10) {
            for (int i = 1; ; i++) {
                UserInfo userInfo = userInfoService.findByUserId(RandomUtil.randomLong(1, 100));
                if (userInfo == null) {
                    continue;
                }
                if (userInfo.getId().equals(UserHolder.getUserId())) {
                    System.out.println("随机赋值");
                    continue;
                }
                SimilarOne similarOne = new SimilarOne();
                similarOne.setAvatar(userInfo.getAvatar());
                similarOne.setId(userInfo.getId().intValue());
                similarOnes.add(similarOne);
                if (similarOnes.size() == 10) {
                    break;
                }
            }
        }
        soulResultVo.setSimilarYou(similarOnes);
        System.out.println("soulResultVo--" + soulResultVo);

        return ResponseEntity.ok(soulResultVo);


    }
}
